let { Article } = require("../model/article");

// 发布文章
const add = async (ctx) => {
  try {
    let article = ctx.request.body;
    const rel = await Article.create(article);
    if (rel) {
      ctx.body = {
        code: 200,
        msg: "文章发布成功",
      };
    } else {
      ctx.body = {
        code: 400,
        msg: "文章发布失败",
      };
    }
  } catch (err) {
    ctx.body = {
      code: 500,
      msg: err,
    };
  }
};

// 查询所有文章(分页)
const findAll = async (ctx) => {
  try {
    let {page, author} = ctx.query;
    // 判断页码
    if (!page || isNaN(Number(page))) {
      page = 1;
    } else {
      page = Number(page);
    }
    // 每页条数
    let pageSize = 10;
    // 计算总条数
    let count = 0;
    // { author }
    const rel = await Article.find().countDocuments();
    let totalPage = 0;
    if(count > 0) {
        totalPage = Math.ceil(count / pageSize);
    }
    // 判断当前页码的范围
    if (totalPage > 0 && page > totalPage) {
        page = totalPage
    } else if (page < 1) {
        page = 1
    }

    // skip 查询起始位置 起始下标
    let start = (page - 1) * pageSize
    const rel2 = await Article.find({ author }).skip(start).limit(pageSize)
    console.log('rel2', rel2)
    if(rel2 && rel2.length >= 0) {
        ctx.body = {
            code: 200,
            msg: '文章列表查询成功',
            total: rel,
            data: rel2
        }
    } else {
        ctx.body = {
            code: 400,
            msg: '文章列表查询失败'
        }
    }
} catch (error) {
    return (ctx.body = {
      code: 500,
      msg: "服务器错误",
      error,
    });
  }
};
// 查询单个文章
const findOne = async (ctx) => {
    try {
        const { id } = ctx.query
        let isRead = false
        const rel = await Article.findOne({ id })
        if(rel) {
            isRead = true
            ctx.body = {
                code: 200,
                msg: '文章查询成功',
                data: rel
            }
        } else {
            ctx.body = {
                code: 400,
                msg: '文章查询失败'
            }
        }

        if(isRead) {
            await Article.updateOne({ id }, { $inc: { read: 1 } })
        }
    } catch (error) {
        return (ctx.body = {
            code: 500,
            msg: "服务器错误",
            error,
        });
    }
};

// 修改文章
const update = async ctx => {
    try {
        const artcile = ctx.request.body
        const rel = await Article.updateOne({ id: artcile.id }, {
            title: artcile.title,
            stemfrom: artcile.stemfrom,
            content: artcile.content
        })

        if (rel) {
            ctx.body = {
                code: 200,
                msg: '文章修改成功'
            }
        } else {
            ctx.body = {
                code: 400,
                msg: '文章修改失败'
            }
        }
    }
    catch (error) {
        return (ctx.body = {
            code: 500,
            msg: "服务器错误",
            error,
        });
    }
}

// 删除文章
const del = async ctx => {
    try {
        let { id } = ctx.request.body
        const rel = await Article.deleteOne({ id })
        console.log('rel', rel)
        if (rel.deletedCount > 0) {
            ctx.body = {
                code: 200,
                msg: '文章删除成功'
            }
        } else {
            ctx.body = {
                code: 400,
                msg: '文章删除失败'
            }
        }
    }
    catch (error) {
        return (ctx.body = {
            code: 500,
            msg: "服务器错误",
            error,
        });
    }
}

module.exports = {
  add,
  findAll,
  findOne,
  update,
  del
};
